package com.wisewin.pose.service;

import com.wisewin.pose.config.dto.ResultDTO;
import com.wisewin.pose.config.dto.ResultDTOBuilder;
import com.wisewin.pose.dao.*;
import com.wisewin.pose.entity.bo.QueryParamBO;
import com.wisewin.pose.entity.db.UserDO;
import com.wisewin.pose.util.PageInfo;
import com.wisewin.pose.util.PageObjectUtil;
import com.wisewin.pose.util.PageUtil;
import com.wisewin.pose.util.wechat.WechatOfficeUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class UserService {
	final static Logger log = LoggerFactory.getLogger(UserService.class);
	@Autowired
	private UserDAO userDAO;

	public ResultDTO updateUser(UserDO userDO) {
		if (StringUtils.isEmpty(userDO.getUserName())) {
			return ResultDTOBuilder.failure("1", "请填写用户姓名", null);
		}
		if (userDO.getSex() == null) {
			return ResultDTOBuilder.failure("1", "请选择用户性别", null);
		}
		if (userDO.getBirthday() == null) {
			return ResultDTOBuilder.failure("1", "请选择出生日期", null);
		}
		userDAO.update(userDO);
		return ResultDTOBuilder.success(userDO);
	}

	public UserDO addUser(String openid) {
		// 判断openid是否存在
		UserDO userDO = userDAO.queryByOpenid(openid);
		if (userDO == null) {
			// 添加用户信息
			userDO = new UserDO();
			userDO.setOpenid(openid);
			// 处理机构用户
			userDAO.insert(userDO);
		}
		return userDO;
	}

	public UserDO queryByPhone(String phone) {
		return userDAO.queryByPhone(phone);
	}

	public UserDO queryByOpenId(String openid) {
		return userDAO.queryByOpenid(openid);
	}

	/**
	 * 获取用户列表
	 */
	public ResultDTO queryUserPage(QueryParamBO queryParamBO, PageUtil pageUtil) {
		Map<String, Object> map = new HashMap<>();
		map.put("orgId", queryParamBO.getOrgId());
		map.put("userName", queryParamBO.getUserName());
		map.put("startTime", queryParamBO.getStartTime());
		map.put("endTime", queryParamBO.getEndTime());
		map.put("phone", queryParamBO.getPhone());
		map.put("sex", queryParamBO.getSex());
		map.put("pageOffset", pageUtil.getPageNo());
		map.put("pageSize", pageUtil.getPageSize());

		PageObjectUtil page = new PageObjectUtil();
		PageInfo pageData = page.savePageObject(userDAO.queryUserCount(map), userDAO.queryUserList(map), pageUtil);
		return ResultDTOBuilder.success(pageData);
	}

	public ResultDTO getToken() {
		String token = WechatOfficeUtil.getToken();
		if (StringUtils.isEmpty(token)) {
			return ResultDTOBuilder.failure("1", "获取信息有误", null);
		}
		Map<String, Object> result = new HashMap<>();
		result.put("token", token);
		return ResultDTOBuilder.success(result);
	}

	public ResultDTO getAuthUserInfo(String code) {
		if (StringUtils.isEmpty(code)) {
			return ResultDTOBuilder.failure("1", "参数有误", null);
		}
		// 获取用户信息
		ResultDTO resultDTO = WechatOfficeUtil.getUserInfo(code);
		if ("1".equals(resultDTO.getCode())) {
			return resultDTO;
		}
		UserDO userDO = (UserDO) resultDTO.getData();
		// 判断openid是否存在
		UserDO userOpenid = userDAO.queryByOpenid(userDO.getOpenid());
		if (userOpenid == null) {
			// 添加用户信息
			userDO.setUserName(userDO.getNickname());
			userDO.setOrgId(1);
			userDAO.insert(userDO);
		} else {
			// 更新用户信息
			userDO.setUserName(userOpenid.getUserName());
			userDO.setId(userOpenid.getId());
			userDAO.updateAuth(userDO);
		}
		return ResultDTOBuilder.success(userDO);
	}

	public ResultDTO updateAuthUserInfo(QueryParamBO param) {
		if (StringUtils.isEmpty(param.getUserName())) {
			return ResultDTOBuilder.failure("1", "参数有误", null);
		}
		if (param.getBirthday() == null) {
			return ResultDTOBuilder.failure("1", "参数有误", null);
		}
		String openId = param.getOpenid();
		UserDO userDO;
		if (StringUtils.isEmpty(openId)) {
			// 无openid 更新
			if (StringUtils.isEmpty(param.getCode())) {
				return ResultDTOBuilder.failure("1", "参数有误", null);
			}
			ResultDTO resultDTO = WechatOfficeUtil.getOpenId(param.getCode());
			if ("1".equals(resultDTO.getCode())) {
				return resultDTO;
			}
			userDO = (UserDO) resultDTO.getData();
			openId = userDO.getOpenid();
		} else {
			// 有openid 更新
			userDO = new UserDO();
			userDO.setOpenid(openId);
		}

		// 判断openid是否存在
		UserDO userOpenid = userDAO.queryByOpenid(openId);
		if (userOpenid == null) {
			// 添加用户信息
			userDO.setUserName(param.getUserName());
			userDO.setBirthday(param.getBirthday());
			userDO.setSex(param.getSex());
			userDAO.insert(userDO);
		} else {
			// 更新用户信息
			userDO.setUserName(param.getUserName());
			userDO.setBirthday(param.getBirthday());
			userDO.setSex(param.getSex());
			userDO.setId(userOpenid.getId());
			userDAO.update(userDO);
		}
		return ResultDTOBuilder.success(userDO);
	}

	public ResultDTO checkUserInfo(QueryParamBO param) {
		if (StringUtils.isEmpty(param.getCode())) {
			return ResultDTOBuilder.failure("1", "参数有误", null);
		}
		ResultDTO resultDTO = WechatOfficeUtil.getOpenId(param.getCode());
		if ("1".equals(resultDTO.getCode())) {
			return resultDTO;
		}
		UserDO userDO = (UserDO) resultDTO.getData();
		log.info("userDO " + userDO);
		// 判断openid是否存在
		UserDO userOpenid = userDAO.queryByOpenid(userDO.getOpenid());
		if (userOpenid == null) {
			return ResultDTOBuilder.failure("1", "未授权");
		}
		if (StringUtils.isEmpty(userOpenid.getUserName()) && userOpenid.getBirthday() == null) {
			return new ResultDTO(true, "0000001", "未完善信息", userOpenid);
		}
		return ResultDTOBuilder.success(userOpenid);

	}

	public ResultDTO queryUserById(Integer userId) {
		if (userId == null) {
			return ResultDTOBuilder.failure("1", "参数有误", null);
		}
		UserDO userDO = userDAO.queryUserById(userId);
		return ResultDTOBuilder.success(userDO);
	}
}
